﻿

Module modHaupt

    Friend Function Main() As Integer

        Dim Result As Integer = 0
        Try
            Dim CommandParams As Dictionary(Of String, String) = CommandlineToDictionary()

            Select Case True
                Case CommandParams.ContainsKey("x") AndAlso CommandParams.ContainsKey("w") 'Error
                    Throw New ApplicationException("Switches /x and /w are mutually exclusive.")
                Case CommandParams.ContainsKey("h") 'Show help
                    Console.WriteLine("Syntax:")
                    Console.WriteLine("MinecraftToXML.exe [ /h | [ [/x | /w] /i=InputPath /o=OutputPath [ /y ] ]")
                    Console.WriteLine()
                    Console.WriteLine()
                    Console.WriteLine("Commands:")
                    Console.WriteLine("{0} - {1}", "h", "Show this help.")
                    Console.WriteLine("{0} - {1}", "x", "Convert a Minecraft world to XML (/i and /o parameters are required).")
                    Console.WriteLine("{0} - {1}", "w", "Convert an XML to a Minecraft world (/i and /o parameters are required).")
                    Console.WriteLine()
                    Console.WriteLine()
                    Console.WriteLine("Parameters:")
                    Console.WriteLine("{0} - {1}", "i", "Specifies the input path from which will be read. Enclose in parantheses if it contains spaces.")
                    Console.WriteLine("{0} - {1}", "o", "Specifies the output path to which the converter will write. Enclose in parantheses if it contains spaces.")
                    Console.WriteLine("{0} - {1}", "y", "Overwrite existing files in output path.")
                    Result = 1
                Case CommandParams.ContainsKey("x") 'Create XML from World
                    If CommandParams.ContainsKey("i") AndAlso CommandParams.ContainsKey("o") Then
                        CreateXMLFromWorld(CommandParams("i"), CommandParams("o"), CommandParams.ContainsKey("y"))
                    Else
                        Throw New ApplicationException("Command /x requires Parameters /i and /o to be set.")
                    End If
                Case CommandParams.ContainsKey("w") 'Create World from XML
                    If CommandParams.ContainsKey("i") AndAlso CommandParams.ContainsKey("o") Then
                        CreateWorldFromXML(CommandParams("i"), CommandParams("o"), CommandParams.ContainsKey("y"))
                    Else
                        Throw New ApplicationException("Command /w requires Parameters /i and /o to be set.")
                    End If
            End Select

        Catch ex As Exception
            Console.WriteLine("ERROR: {0}", ex.Message)
            Console.WriteLine("HINT: Use 'MinecraftToXML /H' for help.")
            Result = 1
        End Try

#If DEBUG Then
        Console.WriteLine("DEBUG MODE: Hit any key.")
        Console.ReadKey()
#End If


        Return Result
    End Function

    Private Function CommandlineToDictionary() As Dictionary(Of String, String)
        Dim r As New System.Text.RegularExpressions.Regex("^/(?<Switch>[A-Za-z])(?:[A-Za-z])*([:=](?<Value>.*))?$")
        Dim Result As New Dictionary(Of String, String)

        For Each s As String In My.Application.CommandLineArgs
            Dim m As System.Text.RegularExpressions.Match = r.Match(s)
            If m.Success Then
                Result.Add(m.Groups("Switch").Value.ToLower, m.Groups("Value").Value.Trim("""".ToCharArray))
            End If
        Next

        Return Result
    End Function

End Module
